//
//  SureOrderViewController.m
//  Manager
//
//  Created by 彭凯敏 on 15/8/18.
//  Copyright (c) 2015年 52xiaoluo. All rights reserved.
//

#import "SureOrderViewController.h"
#import "MenuTabBar.h"
#import "SearchViewController.h"
#import "SearchCell.h"
@interface SureOrderViewController ()<searchDelegate>


@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;

@property (weak, nonatomic) IBOutlet UIView *contentView;

@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
@property (weak, nonatomic) IBOutlet UILabel *expectLabel;

//timeView
- (IBAction)clickTimeView:(UITapGestureRecognizer *)sender;
@property (weak, nonatomic) IBOutlet UIView *timeView;
@property (weak, nonatomic) IBOutlet UILabel *timeChooseLabel;
@property (strong, nonatomic) IBOutlet UIView *pickView;
@property (weak, nonatomic) IBOutlet UIDatePicker *datePicker;
- (IBAction)dateChanged:(UIDatePicker *)sender;
- (IBAction)clickPickSure:(UIButton *)sender;


@property (weak, nonatomic) IBOutlet UITextField *room1TF;
@property (weak, nonatomic) IBOutlet UILabel *roomStatus1;
@property (weak, nonatomic) IBOutlet UITextField *room2TF;
@property (weak, nonatomic) IBOutlet UILabel *roomStatus2;


- (IBAction)clickSure:(UIButton *)sender;
//search
@property (strong, nonatomic) UITableView *searchTableView;
@property (strong, nonatomic) NSMutableArray *searchDataArray;
@property (strong, nonatomic) NSMutableArray *roomArray;
@property (strong, nonatomic) UITextField *currentTF;


@end

@implementation SureOrderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self baseCtlSetTitle:[_typeStr integerValue] == 0? @"确认看房":@"用户重新预约"];
    
    _roomArray = [[NSMutableArray alloc]initWithObjects:@"",@"", nil];
    _scrollView.contentSize = CGSizeMake(ScreenWidth, IS320?554: ScreenHeight-63.5);
    
     [_contentView setSizeWidth:ScreenWidth];
    [_scrollView addSubview:_contentView];
    
    
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(requestSuccess:) name:Notifi_ConfirmView object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(requestSuccess:) name:CMD_ViewRoom_Review object:nil];
    
    
    [self resetUI];
}

- (void)resetUI
{

    
    if (!_orderInfo) {
        return;
    }
    
    _nameLabel.text = [_orderInfo.name judgeWithBase:@""];
    _timeLabel.text = [NSString stringWithFormat:@"预约时间：%@",[KMGetDate timeFromNum:[NSString stringWithFormat:@"%llu",_orderInfo.viewTime/1000] andFormStr:@"YYYY-MM-dd HH:mm"]];
    _expectLabel.attributedText = [[NSString stringWithFormat:@"%d",(int)_orderInfo.expectPrice]judgeAttributePrice:@"心理价位："];
    
    
    
    _timeChooseLabel.text = [KMGetDate timeFromNum:[NSString stringWithFormat:@"%llu",_orderInfo.viewTime/1000] andFormStr:@"YYYY/MM/dd HH:mm"];
    
    if (_orderInfo.viewRooms) {
        
        RoomInfo *info = _orderInfo.viewRooms[0];
        
        _room1TF.text = info.name;
        _roomStatus1.text = [UIViewCategory roomStatusWith:info];
        [_roomArray replaceObjectAtIndex:0 withObject:info];
        
        if (_orderInfo.viewRooms.count == 2) {
            RoomInfo *info2 = _orderInfo.viewRooms[1];
            
            _room2TF.text = info2.name;
            _roomStatus2.text = [UIViewCategory roomStatusWith:info2];
            [_roomArray replaceObjectAtIndex:1 withObject:info2];
        }

    }
    
    
    
}

#pragma mark - request

- (void)RoomSearchReqWithtext:(NSString *)text
{
    RoomSearchReqBuilder *builder = [[RoomSearchReqBuilder alloc]init];
    RoomFilterBuilder *filter = [[RoomFilterBuilder alloc]init];
    [filter setName:text];
    
    [builder setRoomFilter:filter.build];
    
    [[ViewRoomLogic shareInstance]roomSearchReqWithBuild:builder.build];
    
}
- (void)ConfirmViewReq
{
    for (NSInteger i =0;i<_roomArray.count;i++) {
        id object = _roomArray[i];
        if ([object isKindOfClass:[NSString class]]) {
            [_roomArray removeObject:object];
        }
    }
    if (_roomArray.count == 0) {
        FailedShow(@"请输入正确的房源信息");
        return;
    }

    
    ConfirmViewReqBuilder *builder = [[ConfirmViewReqBuilder alloc]init];
    [builder setIsView:YES];
    
    [builder setViewOrderId:_orderInfo.id];
    

    [builder setViewTime:[[KMGetDate timeSpFromDate:_datePicker.date] longLongValue]*1000];
    
    [builder setViewStewardId:[[UserLogic shareInstance]getUserId]];
    

    [builder setViewRoomsArray:_roomArray];
    
//    [builder setViewRoomsArray:_orderInfo.viewRooms];
    
    if ([_typeStr integerValue] == 0) {
        
        [[ViewRoomLogic shareInstance]confirmViewReqWithBuild:builder.build];
    }
    else
    {
        [[ViewRoomLogic shareInstance]reViewReqWithBuild:builder.build];
    }
    
    
    
}
- (void)requestSuccess:(NSNotification *)noti
{
    dispatch_async(dispatch_get_main_queue(), ^{
        if ([noti.name isEqualToString:Notifi_ConfirmView]) {
            HUDRemove;
            
            if ([noti.object isKindOfClass:[ErrorModel class]])
            {
                ErrorModel *model = noti.object;
                [UIViewCategory FailShowWith:model];
                return;
            }
            else
            {
                
                SuccessShow(@"预约成功");
                self.tabBarController.selectedIndex = 1;
                [self.navigationController popToRootViewControllerAnimated:NO];
            }
        }
        if ([noti.name  isEqualToString: CMD_ViewRoom_Review]) {
            
            HUDRemove;
            if ([noti.object isKindOfClass:[ErrorModel class]])
            {
                ErrorModel *model = noti.object;
                [UIViewCategory FailShowWith:model];
            }
            else
            {
                SuccessShow(@"预约成功");
                
                [self.navigationController popToRootViewControllerAnimated:YES];
                
            }
            
            
        }
        
        
        
    });

}

#pragma mark - UIScrollViewDelegate
- (void)searchSelectRoom:(RoomInfo *)info
{
    _currentTF.text = info.name;
    if (_currentTF == _room1TF) {
        [_roomArray replaceObjectAtIndex:0 withObject:info];
        _roomStatus1.text = [UIViewCategory roomStatusWith:info];
    }
    if (_currentTF == _room2TF) {
        [_roomArray replaceObjectAtIndex:1 withObject:info];
        _roomStatus2.text = [UIViewCategory roomStatusWith:info];
    }
    _currentTF = nil;
    
}
- (void)searchNoRoom
{
    _currentTF.text = @"";
    if (_currentTF == _room1TF) {
        [_roomArray replaceObjectAtIndex:0 withObject:@""];
        _roomStatus1.text = @"";
    }
    if (_currentTF == _room2TF) {
        [_roomArray replaceObjectAtIndex:1 withObject:@""];
        _roomStatus2.text = @"";
    }
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{

    [self.view endEditing:YES];
}

#pragma mark - UITextFieldDelegate

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    if (textField == _room1TF || textField == _room2TF) {
        _currentTF = textField;
        
        SearchViewController *search = [[SearchViewController alloc]init];
        
        search.searchStr = textField.text;
        search.delegate = self;
        
        [self.navigationController pushViewController:search animated:YES];
        
    }
    return  YES;
}



#pragma mark - events
- (IBAction)clickSure:(UIButton *)sender {
    if ([_timeChooseLabel.text isEqualToString:@""]) {
        FailedShow(@"请选择预约时间");
        return;
    }
    if ([_room1TF.text isEqualToString:@""]&&[_room2TF.text isEqualToString:@""]) {
        FailedShow(@"请输入房源信息");
        return;
    }
    HUDShow(nil);
    [self ConfirmViewReq];
    
}



#pragma mark - time
- (IBAction)clickTimeView:(UITapGestureRecognizer *)sender {
    [self .view endEditing:YES];
    
    if (![_timeChooseLabel.text isEqualToString:@""]) {
        [_datePicker setDate:[KMGetDate dateFromTimeStr:_timeChooseLabel.text andFormatter:@"yyyy/MM/dd HH:mm"] animated:YES];
    }
    
    [UIViewCategory offsetText:_timeView Height:IS320?225:280 Super:_scrollView];
    if (_pickView.originY == ScreenHeight-64-_pickView.sizeHeight) {
        return;
    }
    
    
    _datePicker.minimumDate = [NSDate date];
    
    [_pickView setOriginY:ScreenHeight-64];
    [_pickView setSizeWidth:ScreenWidth];
    [_pickView setSizeHeight:IS320?225:280];
    [self.view addSubview:_pickView];
    [UIView animateWithDuration:0.3 animations:^{
        
        [_pickView setOriginY:ScreenHeight-64-_pickView.sizeHeight];
    }];
}
- (IBAction)dateChanged:(UIDatePicker *)sender {
    
    
    NSString *timeStr = [self stringFromDate:sender.date];
    _timeChooseLabel.text = timeStr;
    
    
}

- (IBAction)clickPickSure:(UIButton *)sender {
    [self dateChanged:_datePicker];
    
    [UIView animateWithDuration:0.3 animations:^{
        
        [_pickView setOriginY:ScreenHeight-64];
    }];
    
}
- (NSString *)stringFromDate:(NSDate *)date{
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    
    //zzz表示时区，zzz可以删除，这样返回的日期字符将不包含时区信息。
    
    [dateFormatter setDateFormat:@"yyyy/MM/dd HH:mm"];
    
    NSString *destDateString = [dateFormatter stringFromDate:date];
    
    return destDateString;
}
@end
